//
//  KASexPusher.swift
//  kano
//
//  Created by Sophia on 2024/8/22.
//

import UIKit
import BRPickerView

let applyFormat = "dd/MM/yyyy"

class KASexPusher: KAPushView {
    
    var BLOCK_clickBlock : ((String, String) -> Void)?
    
    var var_selectButton: UIButton? = nil
    
    var var_selectBirthday: String = "01/01/1992"
    
    public var sexLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(18), weight: .bold)
        label.textColor = .color323135
        label.text = "Gender"
        return label
    }()
    
    public var birthLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(18), weight: .bold)
        label.textColor = .color323135
        label.text = "Birthday"
        return label
    }()
    
    lazy var boyBtn: UIButton = {
        let button = UIButton()
        button.tag = 0
        button.setImage(UIImage(named: "sex_boy"), for: .normal)
        button.addTarget(self, action: #selector(ka_sexBtnClick(button:)), for: .touchUpInside)
        return button
    }()
    
    lazy var girlBtn: UIButton = {
        let button = UIButton()
        button.tag = 1
        button.setImage(UIImage(named: "sex_girl"), for: .normal)
        button.addTarget(self, action: #selector(ka_sexBtnClick(button:)), for: .touchUpInside)
        return button
    }()
    
    @objc func ka_sexBtnClick(button: UIButton) {
        var_selectButton = button
        if button.tag == 0 {
            girlBtn.ka_removeCornerBorder()
            boyBtn.ka_addCornerBorder(withRadius: kscale(28), color: UIColor.black)
            
            let btnInfo = KABtnInfo(eleId: "sx_acb_idcard_male", pageId: "sx_acv_idcard", clickTime: Date.ka_currentData())
            KAReport.ka_reportWith(type: .btn, data: btnInfo.kj.JSONObject())
        } else {
            boyBtn.ka_removeCornerBorder()
            girlBtn.ka_addCornerBorder(withRadius: kscale(28), color: UIColor.black)
            
            let btnInfo = KABtnInfo(eleId: "sx_acb_idcard_female", pageId: "sx_acv_idcard", clickTime: Date.ka_currentData())
            KAReport.ka_reportWith(type: .btn, data: btnInfo.kj.JSONObject())
        }
    }
    
    var gender: String = "" {
        willSet {
            if newValue == "Male" {
                girlBtn.ka_removeCornerBorder()
                boyBtn.ka_addCornerBorder(withRadius: kscale(28), color: UIColor.black)
                var_selectButton = boyBtn
            } else if newValue == "Female" {
                boyBtn.ka_removeCornerBorder()
                girlBtn.ka_addCornerBorder(withRadius: kscale(28), color: UIColor.black)
                var_selectButton = girlBtn
            } else {
                boyBtn.ka_removeCornerBorder()
                boyBtn.ka_removeCornerBorder()
            }
        }
    }
    
    var birthday: String = "" {
        willSet {
            datePicker.selectDate = Date.ka_date(from: newValue.isEmpty ? var_selectBirthday : newValue, format: applyFormat)
        }
    }
    
    lazy var datePicker:BRDatePickerView = {
        let var_datePickerView = BRDatePickerView()
        var_datePickerView.calendar = Calendar(identifier: .gregorian)
        var_datePickerView.pickerMode = .YMD
        var_datePickerView.title = ""
        var_datePickerView.minDate = Date.ka_date(from: "01/01/1953", format: applyFormat)
        var_datePickerView.maxDate = Date()
        var_datePickerView.selectDate = Date.ka_date(from: var_selectBirthday, format: applyFormat)
        var_datePickerView.isAutoSelect = true
        var_datePickerView.resultBlock = { [self] selectDate, selectValue in
            var_selectBirthday = Date.ka_string(with: selectDate!, format: applyFormat)
            
            let btnInfo = KABtnInfo(eleId: "sx_acb_idcard_birthday", pageId: "sx_acv_idcard", clickTime: Date.ka_currentData())
            KAReport.ka_reportWith(type: .btn, data: btnInfo.kj.JSONObject())
        }
        return var_datePickerView
    }()
    
    lazy var var_bgView: UIView = {
        let var_bgView = UIView();
        var_bgView.backgroundColor = .white
        return var_bgView
    }()
//    private let datePicker: UIDatePicker = {
//        let picker = UIDatePicker()
//        picker.datePickerMode = .date
//        if #available(iOS 13.4, *) {
//            picker.preferredDatePickerStyle = .wheels
//        } else {
//            // Fallback on earlier versions
//        }
////        picker.translatesAutoresizingMaskIntoConstraints = false
//        picker.addTarget(self, action: #selector(timePickerValueChanged), for: .valueChanged)
//        return picker
//    }()
//    @objc private func timePickerValueChanged(_ sender: UIDatePicker) {
//        let formatter = DateFormatter()
//        formatter.timeStyle = .short
//        let timeString = formatter.string(from: sender.date)
//        print("Selected time: \(timeString)")
//    }
    
    lazy var var_okBtn: UIButton = {
        let button = UIButton()
        button.setTitle("OK", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: kscale(17), weight: .bold)
        button.backgroundColor = .themeColor
        button.layer.cornerRadius = kscale(25)
        button.layer.masksToBounds = true
        button.addTarget(self, action: #selector(ka_okBtnClick), for: .touchUpInside)
        return button
    }()
    
    @objc func ka_okBtnClick() {
        if var_selectButton == nil {
            makeToast("Please select gender"); return
        }
        if BLOCK_clickBlock != nil {
            let btnInfo = KABtnInfo(eleId: "sx_acb_idcard_next", pageId: "sx_acv_idcard", clickTime: Date.ka_currentData())
            KAReport.ka_reportWith(type: .btn, data: btnInfo.kj.JSONObject())
            
            BLOCK_clickBlock!(var_selectButton!.tag == 0 ? "Male" : "Female", var_selectBirthday)
            ka_dismiss()
        }
    }
    
    init(clickBlock: ((String, String) -> Void)? = nil) {
        self.BLOCK_clickBlock = clickBlock
        super.init(frame: CGRect(x: 0, y: 0, width: var_screenWidth, height: var_screenHeight))
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func ka_initSubViews() {
        
        backTapEnable = false
        var_shadowView.frame = CGRectMake(0.0, bounds.size.height, bounds.size.width, kscale(500)+UIDevice.ka_safeBottom)
        var_contentView.frame = var_shadowView.bounds
        var_contentView.backgroundColor = .white
//        var_backgroundView.backgroundColor = UIColor(0x000000, alphaVaule: 0.4)
        var_titleLabel.isHidden = true
        var_doneButton.isHidden = true
        var_closeButton.isHidden = true
        var_backgroundView.alpha = 0.1
        //add content
        var_contentView.addSubview(sexLabel)
        var_contentView.addSubview(boyBtn)
        var_contentView.addSubview(girlBtn)
        var_contentView.addSubview(birthLabel)
        var_contentView.addSubview(datePicker)
        var_contentView.addSubview(var_bgView)
        var_contentView.addSubview(var_okBtn)

        sexLabel.snp.makeConstraints { make in
            make.centerX.equalTo(var_contentView)
            make.top.equalTo(var_contentView).offset(kscale(32))
        }
        boyBtn.snp.makeConstraints { make in
            make.width.height.equalTo(kscale(108))
            make.top.equalTo(sexLabel.snp.bottom).offset(kscale(16))
            make.right.equalTo(var_contentView.snp.centerX).offset(-kscale(25))
        }
        girlBtn.snp.makeConstraints { make in
            make.size.equalTo(boyBtn.snp.size)
            make.top.equalTo(boyBtn.snp.top)
            make.left.equalTo(var_contentView.snp.centerX).offset(kscale(25))
        }
        
        birthLabel.snp.makeConstraints { make in
            make.centerX.equalTo(var_contentView)
            make.top.equalTo(girlBtn.snp.bottom).offset(kscale(32))
        }
        
        var_bgView.snp.makeConstraints { make in
            make.left.equalTo(var_contentView).offset(kscale(20))
            make.right.equalTo(var_contentView).offset(kscale(-20))
            make.top.equalTo(birthLabel.snp.bottom).offset(kscale(16))
            make.height.equalTo(kscale(140))
        }
        var_okBtn.snp.makeConstraints { make in
            make.left.equalTo(var_contentView).offset(kscale(68))
            make.right.equalTo(var_contentView).offset(kscale(-68))
            make.top.equalTo(datePicker.snp.bottom).offset(kscale(20))
            make.height.equalTo(kscale(50))
        }
        datePicker.addPicker(to: var_bgView)
    }
}

extension KASexPusher {
    
}
